package com.wula.gtsns.dao;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;

import com.wula.gtsns.entity.MemberInfo;

import static org.hibernate.criterion.Example.create;

/**
 * A data access object (DAO) providing persistence and search support for
 * MemberInfo entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.wula.gtsns.entity.MemberInfo
 * @author MyEclipse Persistence Tools
 */

public class MemberInfoDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(MemberInfoDAO.class);
	// property constants
	public static final String JOB = "job";
	public static final String COUNTRY = "country";
	public static final String CITY = "city";
	public static final String AREA = "area";
	public static final String LANGUAGE = "language";
	public static final String AGE = "age";
	public static final String PROVINCE = "province";
	public static final String GENDER = "gender";
	public static final String JID = "jid";
	public static final String SEARCH_BY_JID = "searchByJid";

	public void save(MemberInfo transientInstance) {
		log.debug("saving MemberInfo instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(MemberInfo persistentInstance) {
		log.debug("deleting MemberInfo instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public MemberInfo findById(java.lang.Integer id) {
		log.debug("getting MemberInfo instance with id: " + id);
		try {
			MemberInfo instance = (MemberInfo) getSession().get(
					"com.wula.gtsns.orm.MemberInfo", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<MemberInfo> findByExample(MemberInfo instance) {
		log.debug("finding MemberInfo instance by example");
		try {
			List<MemberInfo> results = (List<MemberInfo>) getSession()
					.createCriteria("com.wula.gtsns.orm.MemberInfo").add(
							create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding MemberInfo instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from MemberInfo as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<MemberInfo> findByJob(Object job) {
		return findByProperty(JOB, job);
	}

	public List<MemberInfo> findByCountry(Object country) {
		return findByProperty(COUNTRY, country);
	}

	public List<MemberInfo> findByCity(Object city) {
		return findByProperty(CITY, city);
	}

	public List<MemberInfo> findByArea(Object area) {
		return findByProperty(AREA, area);
	}

	public List<MemberInfo> findByLanguage(Object language) {
		return findByProperty(LANGUAGE, language);
	}

	public List<MemberInfo> findByAge(Object age) {
		return findByProperty(AGE, age);
	}

	public List<MemberInfo> findByProvince(Object province) {
		return findByProperty(PROVINCE, province);
	}

	public List<MemberInfo> findByGender(Object gender) {
		return findByProperty(GENDER, gender);
	}

	public List<MemberInfo> findByJid(Object jid) {
		return findByProperty(JID, jid);
	}

	public List<MemberInfo> findBySearchByJid(Object searchByJid) {
		return findByProperty(SEARCH_BY_JID, searchByJid);
	}

	public List findAll() {
		log.debug("finding all MemberInfo instances");
		try {
			String queryString = "from MemberInfo";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public MemberInfo merge(MemberInfo detachedInstance) {
		log.debug("merging MemberInfo instance");
		try {
			MemberInfo result = (MemberInfo) getSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(MemberInfo instance) {
		log.debug("attaching dirty MemberInfo instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(MemberInfo instance) {
		log.debug("attaching clean MemberInfo instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}